<?php

declare (strict_types = 1);

namespace own\middleware;

use own\helps\JwtAuth;
use own\helps\RespCode;
use own\interface\OwnMiddlewareInterface;
use think\Exception;
use think\Response;

class FooMiddleware extends OwnMiddlewareInterface
{
    /**
     * 处理请求信息
     * @param Request $request
     * @param string $token
     * @param Closure  $next
     * @return Response
     */
    public function handleRequest($request, $token, $next)
    {
        $request->port = 2; //端口：1管理端api 2客户端api
        $request->uid = 0;  //未登录
        $request->auth = [];//无账户信息
        if($token) {
            try {
                $user = make(JwtAuth::class)->verify($token);
                $request->uid = $user ? (int) $user->id : 0;
                $request->auth = $user ?: [];
            } catch (Exception $e) {
               tips('请重新登录', RespCode::NOT_LOGIN);
            }
        }
        return $next($request);
    }
}
